#include "Log.hpp"
#include <iostream>
#include <memory>
#include "InetArr.hpp"

namespace SocketModule
{
    using namespace LogModule;
    class Socket;
    const int gdefaultsockfd = -1;

    using SockPtr = std::shared_ptr<Socket>;
    // 基类，规定创建socket的方法
    // 提供一个/若干个/固定模式的socket的方法
    // 模版方法模式
    class Socket
    {
    public:
        virtual ~Socket() = default;
        virtual void SocketOrDie() = 0;
        virtual void SetSocketOpt() = 0;
        virtual bool BindOrDie(int port) = 0;
        virtual bool ListenOrDie() = 0;
        virtual int Accepter(InetAddr *client) = 0;
        virtual void Close() = 0;
        virtual int Recv(std::string *out) = 0;
        virtual int Send(const std::string &in) = 0;
        virtual int Fd() = 0;
        // 提供一个创建listensockfd的固定套路
        void BuildTcpSocketMethod(int port)
        {
            if (port == gdefaultsockfd)
            {
                LOG(LogLevel::ERROR) << "port is -1";
                return;
            }

            SocketOrDie();
            SetSocketOpt();
            BindOrDie(port);
            ListenOrDie();
        }
    };

    class TcpSocket : public Socket
    {
    public:
        TcpSocket(int sockfd = gdefaultsockfd)
            : _sockfd(sockfd)
        {
        }
        ~TcpSocket() {}
        void SocketOrDie() override
        {
            _sockfd = socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd == -1)
            {
                LOG(LogLevel::ERROR) << "socket failed";
                DIE(SOCKET_ERROR);
            }
            LOG(LogLevel::DEBUG) << "socket success";
        }

        void SetSocketOpt() override
        {
            int opt = 1;
            int n = ::setsockopt(_sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
            (void)n;
        }
        bool BindOrDie(int port) override
        {
            InetAddr inet(port);
            int n = bind(_sockfd, inet.NetAddr(), inet.NetAddrLen());
            if (n == -1)
            {
                LOG(LogLevel::ERROR) << "bind failed";
                DIE(BIND_ERROR);
            }
            LOG(LogLevel::DEBUG) << "bind success";
            return true;
        }

        bool ListenOrDie() override
        {
            int n = listen(_sockfd, 8);
            if (n == -1)
            {
                LOG(LogLevel::ERROR) << "listen failed";
                DIE(LISTEN_ERROR);
            }
            LOG(LogLevel::DEBUG) << "listen success";
            return true;
        }
        virtual void Close() override
        {
            if (_sockfd == gdefaultsockfd)
                return;
            ::close(_sockfd);
        }

        virtual int Accepter(InetAddr *client) override
        {
            struct sockaddr_in peer;
            socklen_t peerLen = sizeof(peer);
            int newsockfd = accept(_sockfd, CONV(&peer), &peerLen);
            if (newsockfd == -1)
            {
                LOG(LogLevel::ERROR) << "accept failed";
            }
            *client = InetAddr(peer);

            return newsockfd;
        }

        virtual int Recv(std::string *out) override
        {
            char buffer[1024 * 8];
            auto size = ::recv(_sockfd, buffer, sizeof(buffer) - 1, 0);
            if (size > 0)
            {
                buffer[size] = 0;
                *out = buffer;
            }
            return size;
        }
        virtual int Send(const std::string &in) override
        {
            auto size = ::send(_sockfd, in.c_str(), in.size(), 0);
            return size;
        }

        virtual int Fd() override
        {
            return _sockfd;
        }

    private:
        int _sockfd;
    };
}